package com.schoolcloud.common.util;

import cn.hutool.core.util.StrUtil;
import com.schoolcloud.common.exception.LoginUserException;

public class UserContext {
    private static final ThreadLocal<String> tl = new ThreadLocal<>();
    private static final ThreadLocal<String> userTypeTl = new ThreadLocal<>();
    private static final ThreadLocal<Boolean> isAdmin = new ThreadLocal<>();

    public static void setUser(String userId) {
        tl.set(userId);
    }
    public static void setUserType(String userType) {
        userTypeTl.set(userType);
    }
    public static void setIsAdmin(Boolean isAdmin) {
        UserContext.isAdmin.set(isAdmin);
    }
    public static Boolean getIsAdmin(){
        return isAdmin.get();
    }
    public static String getUser() throws LoginUserException {
        if(StrUtil.isBlank(tl.get())){
            throw new LoginUserException();
        }
        return tl.get();
    }
    public static String getUserType() throws LoginUserException {
        if(StrUtil.isBlank(userTypeTl.get())){
            throw new LoginUserException();
        }
        return userTypeTl.get();
    }
    public static Integer getUserToInteger() throws LoginUserException {
        if(StrUtil.isBlank(tl.get())){
            throw new LoginUserException();
        }
        return Integer.valueOf(tl.get());
    }

    public static Long getUserIdToLong() throws LoginUserException {
        if(StrUtil.isBlank(tl.get())){
            throw new LoginUserException();
        }
        return Long.valueOf(tl.get());
    }

    public static void removeUser() {
        tl.remove();
    }
}
